﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OneShoppingDAL;
using OneShoppingModel;
using System.Linq.Expressions;
namespace OneShoppingBLL
{
    public class rolemenuViewBLL
    {
        private static rolemenuViewBLL instance;
        public static rolemenuViewBLL GetInstance()
        {
            if (instance == null)
            {
                instance = new rolemenuViewBLL();
            }
            return instance;
        }
        /// <summary>
        /// 根据Lambda表达式predicate条件，获取 rolemenuView 单一实体
        /// </summary>
        /// <param name="predicate">获取实体的条件</param>
        /// <returns> rolemenuView单一实体</returns>
        public rolemenuView GetEntity(Expression<Func<rolemenuView, bool>> predicate)
        {
            if (predicate != null)
                return LinqUtility.GetInstance().GetEntity<rolemenuView>
                    (new OneShoppingDataContext(),
                predicate);
            else return null;

        }
        /// <summary>
        /// 获取rolemenuView表的所有实体
        /// </summary>
        /// <returns>rolemenuView集合</returns>
        public List<rolemenuView> GetList()
        {
            return LinqUtility.GetInstance().GetList<rolemenuView>
                    (new OneShoppingDataContext());
        }

        /// <summary>
        /// 根据Lambda表达式predicate条件，获取rolemenuView集合
        /// </summary>
        /// <param name="predicate">获取列表的条件</param>
        /// <returns>rolemenuView集合</returns>
        public List<rolemenuView> GetList(Expression<Func<rolemenuView, bool>> predicate)
        {
            if (predicate != null)
                return LinqUtility.GetInstance().GetList<rolemenuView>
                    (new OneShoppingDataContext(), predicate);
            else return null;
        }
        /// <summary>
        /// 根据Lambda表达式predicate条件、每页条数pageSize、页码pageIndex，获取rolemenuView集合
        /// </summary>
        /// <param name="predicate">获取列表的条件</param>
        /// <param name="pageSize">每页条数</param>
        /// <param name="pageIndex">页码</param>
        /// <returns>rolemenuView集合</returns>
        public List<rolemenuView> GetList(Expression<Func<rolemenuView, bool>> predicate,
           int pageSize, int pageIndex)
        {

            if (predicate != null)
                return LinqUtility.GetInstance().GetList<rolemenuView>
                    (new OneShoppingDataContext(), predicate, pageSize, pageIndex);

            else return null;
        }
        /// <summary>
        /// 添加rolemenuView实体
        /// </summary>
        /// <param name="entity">rolemenuView实体</param>
        /// <returns>是否成功添加</returns>
        public bool Insert(rolemenuView entity)
        {
            return
                LinqUtility.GetInstance().InsertEntity<rolemenuView>(new OneShoppingDataContext(), entity);

        }
        /// <summary>
        /// 更新rolemenuView实体
        /// </summary>
        /// <param name="entity">rolemenuView实体</param>
        /// <returns>是否成功更新</returns>
        public bool Update(rolemenuView entity)
        {
            return
                LinqUtility.GetInstance().UpdateEntity<rolemenuView>(new OneShoppingDataContext(), entity);

        }
        /// <summary>
        ///删除某主键id的rolemenuView实体
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns>是否删除成功</returns>
        public bool Delete(int id)
        {

            return LinqUtility.GetInstance().DeleteEntity<rolemenuView>(new OneShoppingDataContext(),
                  k => k.Id == id);
        }

        //根据用户登录的权限  生成系统左侧树型一二级菜单
        public string GenerateTreeCode(int roleid)
        {

            var firstMenuList = rolemenuViewBLL.GetInstance().GetList(r=>r.RoleId==roleid&&r.ParentMenu==0 && r.Status!=0).OrderBy(r => r.MenuDispSeq);
            string code = "<ul>";
            foreach (var r in firstMenuList)
            {
                code = code + "<li><div class=\"li-item\"><em class=\"scm li-ico ic1\"></em>" + r.MenuName + "<span class=\"scm arrow\"></span></div>";
                var secondMenuList = rolemenuViewBLL.GetInstance().GetList(k => k.RoleId==roleid&& k.ParentMenu == r.MenuId &&k.Status!=0).OrderBy(k => k.MenuDispSeq);
                code = code + "<dl>";
                foreach (var t in secondMenuList)
                {
                    code = code + "<dd><a href='" + t.MenuUrl + "' class=\"dd-item\" target='iframe0'>" + t.MenuName + "<span class=\"scm dd-ar\"></span></a></dd>";
                }
                code = code + "</dl></li>";
            }
            return code;

        }

    }
}